package com.atguigu.tingshu.common.login;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.RequestContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;

@Component
@Aspect
public class TsLoginAspect {

    @Autowired
    private RedisTemplate redisTemplate;


    @Around("execution(* com.atguigu.tingshu.*.api.*.*(..)) && @annotation(tsLogin)")
    public Object tsLoginAspect(ProceedingJoinPoint joinPoint, TsLogin tsLogin) throws Throwable {

            //  1. 先获取到 token
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            //  获取到请求对象
            HttpServletRequest request = servletRequestAttributes.getRequest();
            //  获取token数据
            String token = request.getHeader("token");
            //  判断当前注解上的属性是否需要登录
            if (tsLogin.required()) {
                if (StringUtils.isEmpty(token)) {
                    throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
                }

                String key = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
                UserInfo userInfo = (UserInfo) this.redisTemplate.opsForValue().get(key);
                //  判断当前用户是否为空
                if (null == userInfo) {
                    //  抛出异常！
                    throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
                }
            }
            try {
                //  否则，不需要登录！
                if (!StringUtils.isEmpty(token)) {
                    String key = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
                    UserInfo userInfo = (UserInfo) this.redisTemplate.opsForValue().get(key);
                    //  判断当前用户是否为空
                    if (null != userInfo) {
                        //  存储用户Id到本地线程
                        AuthContextHolder.setUserId(userInfo.getId());
                    }
                }
                //  执行目标方法
                return joinPoint.proceed();

            } catch (GuiguException e) {
                throw new RuntimeException(e);
            } finally {
                //  删除本地内存信息！
                AuthContextHolder.removeUserId();
            }
        }
    }



